//
//  AddViewController.m
//  CoreData_通讯录
//
//  Created by z on 15/11/21.
//  Copyright © 2015年 z. All rights reserved.
//

#import "AddViewController.h"
#import "NewView.h"
#import "Person.h"
@interface AddViewController ()<UITextFieldDelegate>

@property(nonatomic, strong) NewView *lowView;
@property(nonatomic, strong) UIButton *addButton;
@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"新建联系人";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth, 64)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    
    
    // 取消button
    UIButton *cancleButton = [UIButton createButtonWithFrame:CGRectMake(0, 20, 50, 50) Title:@"取消" Target:self Action:@selector(cancleAction:)];
    [view addSubview:cancleButton];
    [cancleButton setTitleColor:[UIColor colorWithRed:0.29 green:0.47 blue:1 alpha:1] forState:UIControlStateNormal];
    
    
    // 完成button
    self.addButton = [UIButton createButtonWithFrame:CGRectMake(ViewWidth - 50, 20, 50, 50) Title:@"完成" Target:self Action:@selector(addAction:)];
    [view addSubview:_addButton];
     [self.addButton setTitleColor:[UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1] forState:UIControlStateNormal];
    self.addButton.enabled = NO;
    
    self.lowView = [[NewView alloc] initWithFrame:CGRectMake(0, 64, ViewWidth, ViewHeight - 64)];
    [self.view addSubview:self.lowView];
    

    self.lowView.nameField.delegate = self;
    
}



// 编辑结束
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.addButton.enabled = YES;
    [self.addButton setTitleColor:[UIColor colorWithRed:0.29 green:0.47 blue:1 alpha:1] forState:UIControlStateNormal];
    
}


// 点击空白处
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.lowView.nameField resignFirstResponder];
}


#pragma mark -button方法

// 取消
- (void)cancleAction:(UIButton *)button{
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


// 完成
- (void)addAction:(UIButton *)button{
    // 按条件查找
    NSArray *selectArr   = [Person MR_findByAttribute:@"name" withValue:self.lowView.nameField.text];
    
    if (selectArr.count > 0 || [self.lowView.nameField.text isEqualToString:@""]) {
        NSLog(@"已添加");
        
    } else {
        // 若没有添加  则进行存储
        Person *person= [Person MR_createEntity];
        person.name = self.lowView.nameField.text;
        person.address = self.lowView.sexField.text;
        
        // 进行存储
        [[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];
    }
    
    
    NSLog(@"%@", NSHomeDirectory());
    
    
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
